﻿
#include <iostream>
#include "zyCommandCtrol.h"

BlackMoonUI::zyCommandCtrol::zyCommandCtrol()
{
	SetMember(L"BackColor", (LONG_PTR)GetSysColor(COLOR_BTNFACE));
	SetMember(L"TextColor", 0);
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyCommand_WndProc);
	m_obj->setChildWndProc(zyCommand_ChildWndProc);
	m_obj->setDestroy(zyCommand_Destroy);
}
BlackMoonUI::zyCommandCtrol::~zyCommandCtrol()
{
	
}
void BlackMoonUI::zyCommandCtrol::OnNCDestroy()
{
	HGDIOBJ BackBrush = (HGDIOBJ)GetMember(L"BackBrush");
	if (BackBrush != 0) {
		DeleteObject(BackBrush);
		SetMember(L"BackBrush", 0);
	}
}
void WINAPI BlackMoonUI::zyCommandCtrol::zyCommand_Destroy(zyObject* obj)
{
	zyCommandCtrol* zyc = (zyCommandCtrol*)obj->getPtrData(L"this");
	zyc->OnNCDestroy();
	zyCtrol::zyCtrol_Destroy(obj);
}
LRESULT WINAPI BlackMoonUI::zyCommandCtrol::zyCommand_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
	if (uMsg == WM_NCDESTROY) {
		zyCommandCtrol* zyc = (zyCommandCtrol*)obj->getPtrData(L"this");
		zyc->OnNCDestroy();
	}
	return zyCtrol::zyCtrol_WndProc(obj,hWnd, uMsg, wParam, lParam, bHandled);
}
LRESULT WINAPI BlackMoonUI::zyCommandCtrol::zyCommand_ChildWndProc(zyObject* obj, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	//std::cout << "zyCommandCtrol_ChildWndProcuMsg: "<< message << std::endl;
	if (message == WM_COMMAND)
	{
		short id = LOWORD(wParam);// & 65535); //LOWORD
		int code = HIWORD(wParam);
		if (code == BN_CLICKED) {
			TRegEvent* evtPtr = obj->getRegEvent(L"OnClick");
			if (evtPtr != nullptr) {
				evtPtr->handler<LRESULT>(lParam, (int)id);
				*bHandle = true;
				return 0;
			}
		}
	}
	if (message >= WM_CTLCOLORMSGBOX && message <= WM_CTLCOLORSTATIC) {
		COLORREF color = (COLORREF)(UINT_PTR)obj->getPtrData(L"TextColor");
		HDC hdc = (HDC)wParam;
		SetTextColor(hdc, color);
		HBRUSH BackBrush = (HBRUSH)obj->getPtrData(L"BackBrush");
		if (BackBrush != 0) {
			COLORREF BackColor = (COLORREF)(UINT_PTR)obj->getPtrData(L"BackColor");
			if (BackColor == -1) {
				SetBkMode(hdc, TRANSPARENT);
			}
			else {
				SetBkColor(hdc, BackColor);
			}
			*bHandle = TRUE;
			return (LRESULT)BackBrush;
		}
	}
	return zyCtrol::zyCtrol_ChildWndProc(obj,hWnd, message, wParam, lParam, bHandle);
}
COLORREF BlackMoonUI::zyCommandCtrol::文本色(COLORREF 参数_文本色)
{
	if (参数_文本色 == 0) {
		return (COLORREF)GetMember(L"TextColor");
	}
	else {
		SetMember(L"TextColor", 参数_文本色);
		HBRUSH hBrush = (HBRUSH)GetMember(L"BackBrush");
		if (hBrush == 0) {
			COLORREF color = (COLORREF)GetMember(L"BackColor");
			hBrush = CreateSolidBrush((COLORREF)color);
			SetMember(L"BackBrush", (LONG_PTR)hBrush);
		}
		重画();
		return 参数_文本色;
	}
}
COLORREF BlackMoonUI::zyCommandCtrol::背景色(COLORREF 参数_背景色)
{
	if (参数_背景色 == 0) {
		return (COLORREF)GetMember(L"BackColor");
	}
	else {
		HGDIOBJ hBrush = (HGDIOBJ)GetMember(L"BackBrush");
		if (hBrush != 0)
			DeleteObject(hBrush);
		if (参数_背景色 == -1) {
			hBrush = GetStockObject(NULL_BRUSH);
		}
		else {
			hBrush = (HGDIOBJ)CreateSolidBrush(参数_背景色);
		}
		SetMember(L"BackBrush", (LONG_PTR)hBrush);
		SetMember(L"BackColor", 参数_背景色);
		重画();
		return 参数_背景色;
	}
}